package ar.uba.dc.cmovil.tripplanner.view;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import ar.uba.dc.cmovil.tripplanner.R;
import ar.uba.dc.cmovil.tripplanner.activity.PlacesActivity;
import ar.uba.dc.cmovil.tripplanner.domain.Place;
import ar.uba.dc.cmovil.tripplanner.widget.PlaceItemWidget;

/**
 * Created by anton on 10/2/13.
 */
public class PlacesView extends View {

    private ViewGroup placesContainer;
    private Button addPlaceButton;
    private TextView tripName;
    private TextView tripStartDate;
    private Map<Place, PlaceItemWidget> places;
    private Activity activity;

    public PlacesView(Activity activity) {
        super(activity, null);

        this.activity = activity;
        places = new HashMap<Place, PlaceItemWidget>();
        placesContainer = (ViewGroup) activity.findViewById(R.id.places_container);
        addPlaceButton = (Button) activity.findViewById(R.id.new_place_button);
        tripName = (TextView) activity.findViewById(R.id.trip_name);
        tripStartDate = (TextView) activity.findViewById(R.id.trip_start_date);
    }

    public Button addPlaceButton() {
        return addPlaceButton;
    }

    public void addPlace(Place place) {
        addPlace(place, 0);
    }

    public void addPlace(Place place, int otherUsers) {
        PlaceItemWidget placeWidget = new PlaceItemWidget(activity.getApplicationContext(), place);
        placeWidget.setEventsHandler((PlacesActivity) activity);
        placeWidget.setOtherUsers(otherUsers);
        places.put(place, placeWidget);
        placesContainer.addView(placeWidget);
    }

    public void removePlace(Place place) {
        placesContainer.removeView(places.get(place));
        places.remove(place);
    }

    public void setTripName(String tripName) {
        this.tripName.setText(tripName);
    }

    public void setTripStartDate(String startDate) {
        this.tripStartDate.setText(startDate);
    }

    public void setOtherUsersAtPlace(Place place, int otherUsers) {
        places.get(place).setOtherUsers(otherUsers);
    }
}